<!doctype html>
<html lang="en">
<head>
  
  <meta charset="utf-8" />
  <title>Masonry Column Shift &middot; Isotope </title>
  
  <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  
  <link rel="stylesheet" href="../css/style.css" />
  
  <!-- scripts at bottom of page -->

</head>
<body class="custom-layout-modes ">
  
  <nav id="site-nav">
    <h1><a href="../index.html">Isotope</a></h1>

    <h2>Docs</h2>
    
    <ul>
      
        
          <li><a href="../docs/introduction.html">Introduction</a>
        
      
        
          <li><a href="../docs/options.html">Options</a>
        
      
        
          <li><a href="../docs/methods.html">Methods</a>
        
      
        
          <li><a href="../docs/layout-modes.html">Layout modes</a>
        
      
        
          <li><a href="../docs/filtering.html">Filtering</a>
        
      
        
          <li><a href="../docs/sorting.html">Sorting</a>
        
      
        
          <li><a href="../docs/animating.html">Animating</a>
        
      
        
          <li><a href="../docs/adding-items.html">Adding items</a>
        
      
        
          <li><a href="../docs/extending-isotope.html">Extending Isotope</a>
        
      
        
          <li><a href="../docs/hash-history-jquery-bbq.html">Hash history with jQuery BBQ</a>
        
      
        
          <li><a href="../docs/help.html">Help</a>
        
      
        
          <li><a href="../docs/license.html">License</a>
        
      
    </ul>
    
    <h2>Demos</h2>
    
    <ul>
      
        
          <li><a href="../demos/basic.html">Basic</a>
        
      
        
          <li><a href="../demos/elements-complete.html">Elements Complete</a>
        
      
        
          <li><a href="../demos/elements-partial.html">Elements Partial</a>
        
      
        
          <li><a href="../demos/layout-modes.html">Layout modes</a>
        
      
        
          <li><a href="../demos/filtering.html">Filtering</a>
        
      
        
          <li><a href="../demos/sorting.html">Sorting</a>
        
      
        
          <li><a href="../demos/relayout.html">reLayout</a>
        
      
        
          <li><a href="../demos/adding-items.html">Adding items</a>
        
      
        
          <li><a href="../demos/infinite-scroll.html">Infinite Scroll</a>
        
      
        
          <li><a href="../demos/images.html">Images</a>
        
      
        
          <li><a href="../demos/combination-filters.html">Combination filters</a>
        
      
        
          <li><a href="../demos/hash-history.html">Hash history</a>
        
      
        
          <li><a href="../demos/fluid-responsive.html">Fluid / responsive</a>
        
      
        
          <li><a href="../demos/removing.html">Removing</a>
        
      
    </ul>
    
    <h2>Custom layout modes</h2>
    
    <ul>
      
        
          <li><a href="../custom-layout-modes/centered-masonry.html">Centered Masonry</a>
        
      
        
          <li><a href="../custom-layout-modes/category-rows.html">Category rows</a>
        
      
        
          <li><a href="../custom-layout-modes/masonry-corner-stamp.html">Masonry corner stamp</a>
        
      
        
          <li><a href="../custom-layout-modes/masonry-gutters.html">Masonry gutters</a>
        
      
        
          <li><a href="../custom-layout-modes/spine-align.html">Spine align</a>
        
      
        
          <li><a href="../custom-layout-modes/big-graph.html">BIG Graph</a>
        
      
        
          <li class="current"><a href="#content">Masonry Column Shift</a></li>
        
      
    </ul>
    
    <h2><a href="../tests/index.html">Tests</a></h2>
    
  </nav> <!-- #site-nav -->
  
  <section id="content">
    
      <h1>Masonry Column Shift</h1>
    

    <style>
  .shifty-item {
    width: 210px;
    margin: 10px;
    float: left;
    background: #AAA;
    border: 5px solid white;
  }

  .shifty-item:hover { z-index: 10; }

  .shifty-item.h1 { height: 140px; background: #66F; }
  .shifty-item.h2 { height: 200px; background: #F66; }
  .shifty-item.h3 { height: 240px; background: #6F6; }
  .shifty-item.h4 { height: 280px; background: #FF6; }

</style>

<section id="copy">
  <p>Custom layout mode <code>masonryColumnShift</code>. Enabled columns of a Masonry layout to be shifted and not affect other columns. After the size of an item element has changed, you can trigger a special <code>columnShiftOfItem</code> method that will push the column down. This layout only works with item elements that have the same width</p>

<div class="highlight"><pre><code class="javascript"><span class="kd">var</span> <span class="nx">$container</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#container&#39;</span><span class="p">);</span>

<span class="nx">$container</span><span class="p">.</span><span class="nx">isotope</span><span class="p">({</span>
  <span class="nx">itemSelector</span><span class="o">:</span> <span class="s1">&#39;.shifty-item&#39;</span><span class="p">,</span>
  <span class="nx">layoutMode</span><span class="o">:</span> <span class="s1">&#39;masonryColumnShift&#39;</span>
<span class="p">});</span>

<span class="nx">$container</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;.shifty-item&#39;</span><span class="p">).</span><span class="nx">hover</span><span class="p">(</span>
  <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">css</span><span class="p">({</span> <span class="nx">height</span><span class="o">:</span> <span class="s2">&quot;+=100&quot;</span> <span class="p">});</span>
    <span class="c1">// note that element is passed in, not jQuery object</span>
    <span class="nx">$container</span><span class="p">.</span><span class="nx">isotope</span><span class="p">(</span> <span class="s1">&#39;shiftColumnOfItem&#39;</span><span class="p">,</span> <span class="k">this</span> <span class="p">);</span>
  <span class="p">},</span>
  <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">css</span><span class="p">({</span> <span class="nx">height</span><span class="o">:</span> <span class="s2">&quot;-=100&quot;</span> <span class="p">});</span>
    <span class="nx">$container</span><span class="p">.</span><span class="nx">isotope</span><span class="p">(</span> <span class="s1">&#39;shiftColumnOfItem&#39;</span><span class="p">,</span> <span class="k">this</span> <span class="p">);</span>
  <span class="p">}</span>
<span class="p">);</span>
</code></pre>
</div>


</section>


<div id="container">
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h4"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h3"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h4"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h4"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h3"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h4"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h4"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h3"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h4"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h1"></div>
  <div class="shifty-item h2"></div>

</div> <!-- #container -->


<script src="../js/jquery-1.7.1.min.js"></script>
<script src="../jquery.isotope.min.js"></script>
<script>


  // -------------------------- Masonry Column Shift -------------------------- //
  
  // custom layout mode
  $.Isotope.prototype._masonryColumnShiftReset = function() {
    // layout-specific props
    var props = this.masonryColumnShift = {
      columnBricks: []
    };
    // FIXME shouldn't have to call this again
    this._getSegments();
    var i = props.cols;
    props.colYs = [];
    while (i--) {
      props.colYs.push( 0 );
      // push an array, for bricks in each column
      props.columnBricks.push([])
    }
  };
  
  $.Isotope.prototype._masonryColumnShiftLayout = function( $elems ) {
    var instance = this,
        props = instance.masonryColumnShift;
    $elems.each(function(){
      var $brick  = $(this);
      var setY = props.colYs;

      // get the minimum Y value from the columns
      var minimumY = Math.min.apply( Math, setY ),
          shortCol = 0;

      // Find index of short column, the first from the left
      for (var i=0, len = setY.length; i < len; i++) {
        if ( setY[i] === minimumY ) {
          shortCol = i;
          break;
        }
      }

      // position the brick
      var x = props.columnWidth * shortCol,
          y = minimumY;
      instance._pushPosition( $brick, x, y );
      // keep track of columnIndex
      $.data( this, 'masonryColumnIndex', i );
      props.columnBricks[i].push( this );

      // apply setHeight to necessary columns
      var setHeight = minimumY + $brick.outerHeight(true),
          setSpan = props.cols + 1 - len;
      for ( i=0; i < setSpan; i++ ) {
        props.colYs[ shortCol + i ] = setHeight;
      }

    });
  };
  
 $.Isotope.prototype._masonryColumnShiftGetContainerSize = function() {
    var containerHeight = Math.max.apply( Math, this.masonryColumnShift.colYs );
    return { height: containerHeight };
  };

  $.Isotope.prototype._masonryColumnShiftResizeChanged = function() {
    return this._checkIfSegmentsChanged();
  };
  
  $.Isotope.prototype.shiftColumnOfItem = function( itemElem, callback ) {
    
    var columnIndex = $.data( itemElem, 'masonryColumnIndex' );
    
    // don't proceed if no columnIndex
    if ( !isFinite(columnIndex) ) {
      return;
    }

    var props = this.masonryColumnShift;
    var columnBricks = props.columnBricks[ columnIndex ];
    var $brick;
    var x = props.columnWidth * columnIndex;
    var y = 0;
    for (var i=0, len = columnBricks.length; i < len; i++) {
      $brick = $( columnBricks[i] );
      this._pushPosition( $brick, x, y );
      y += $brick.outerHeight(true);
    }

    // set the size of the container
    if ( this.options.resizesContainer ) {
      var containerStyle = this._masonryColumnShiftGetContainerSize();
      containerStyle.height = Math.max( y, containerStyle.height );
      this.styleQueue.push({ $el: this.element, style: containerStyle });
    }

    this._processStyleQueue( $(columnBricks), callback )

  };

  $(function(){

    var $container = $('#container');

    $container.isotope({
      itemSelector: '.shifty-item',
      layoutMode: 'masonryColumnShift'
    });

    $container.find('.shifty-item').hover(
      function() {
        $(this).css({ height: "+=100" });
        // note that element is passed in, not jQuery object
        $container.isotope( 'shiftColumnOfItem', this );
      },
      function() {
        $(this).css({ height: "-=100" });
        $container.isotope( 'shiftColumnOfItem', this );
      }
    );

  });
</script>
    
    <footer>
      Isotope by <a href="http://desandro.com">David DeSandro</a> / <a href="http://metafizzy.co">Metafizzy</a>
    </footer>
    
  </section> <!-- #content -->
  

</body>
</html>